<template>
	<view class="container">
		<text class="topTitle" >编辑题目</text>
		<view class="textView">
			<textarea class="textarea" v-model="FormData.subjectName" placeholder="请输入题目名称" placeholder-class="place" name="" id="" cols="30" rows="5"></textarea>
		</view>
		<text class="topTitle">编辑答案</text>
		<view class="answerView">
			<view class="answer" v-for="(i , index) in options">
				<text class="option">{{i.optionName}}</text>
				<input type="text" class="text" v-model="i.optionContent">
				<text class="del"  @click='removeOption(i)' >删除</text>
			</view>
			<view class="answer" >
				<button class="add" @click="addOption">增加选项 </button>
			</view>
		</view>
		<text class="topTitle">参考答案</text>
		<view class="answerItem" >
			<text v-for="(i , index) in options" @click="choseAnswer(i)" :class="FormData.answer.answer == i.optionName?'isactive item':'item'" >{{i.optionName}}</text>
		</view>
		<text class="topTitle">答案解析</text>
		<view class="textView">
			<textarea class="textarea"  v-model="FormData.analysis"   placeholder="请输入答案解析" placeholder-class="place" name="" id="" cols="30" rows="8"></textarea>
		</view>
		<uni-row class="uni-row ">
			<uni-col :span="11">
				<text class="topTitle">设定分值</text>
				<view class="input-item">
				  <input type="text"  v-model="FormData.score"  class="text">
				</view>
			</uni-col>
			<uni-col :span="11" :offset="2">
				<text class="topTitle">难易程度</text>
				<view class="input-item">
				 <uni-rate  :value="FormData.level"  :margin="5" />
				</view>
			</uni-col>
		</uni-row>
		<view class="submit-view">
			<button class='submit'  @tap="clickBtn">提交</button>
		</view>
		<uni-popup ref="popup" type="center">
			<view class="popup">
				<text class="topTitle" >新增选项</text>
				<view class="">
					<input class="text" v-model="newOption.optionName"  placeholder="请输入选项" placeholder-class="place" ></input>
					<textarea class="text textarea" v-model="newOption.optionContent" placeholder="请输入选项内容" placeholder-class="place" rows="8"></textarea>
				</view>
				<view class="btnView">
					<button class="button submit" @click="onSure">确定</button>
					<button class="button cancel" @click="close">取消</button>
				</view>
				
			</view>
		</uni-popup>
	</view>
		
</template>
<script>
	import {createSubjectChoices,getSubject} from '@/api/exam/subject.js'
	export default{
		data(){
			return{
				options: [
					{ subjectChoicesId: '', optionName: 'A', optionContent: '' },
					{ subjectChoicesId: '', optionName: 'B', optionContent: '' },
					{ subjectChoicesId: '', optionName: 'C', optionContent: '' },
					{ subjectChoicesId: '', optionName: 'D', optionContent: '' }
				],
				newOption:{ 
					subjectChoicesId: '', 
					optionName: '', 
					optionContent: '' ,
				},
				FormData:{
					subjectName:null,
					answer:{
						answer:'',
						answerType:'',
						score:'',
						subjectId:''
					},
					analysis:null,
					options:null,
					score:5,
					level:3,
					status:0,
					categoryId:null,
					deptId:100,
					choicesType:0,
					type:0
				},
				categoryId:null,
			}
		},
		onLoad(e) {
			this.FormData.categoryId=parseInt(e.categoryId)
			if(e.id != null && e.id != ''){
				this.getData(e.id)
			}
		},
		methods:{
			clickQuuestion(){
				this.$tab.navigateTo(`/pages/work/exam/questionDesc`)
			},
			getData(id){
				getSubject(id,0).then(res=>{
					this.FormData=res.data
					this.options=res.data.options
				})
			},
			onNavigationBarButtonTap(e) {
				this.$refs.popup.open('center')
			},
			removeOption(item) {
				let index = this.options.indexOf(item)
				if (index !== -1) {
					this.options.splice(index, 1)
				}
			},
			close() {
				this.$refs.popup.close()
			},
			addOption(e) {
				this.$refs.popup.open('center')
			},
			onSure(){
				if(this.newOption.optionName == ''){
					this.$modal.msgError("选项不能为空")
				}else{
					this.options.push(this.newOption)
					this.close()
				}
			},
			choseAnswer(e){
				this.FormData.answer.answer = e.optionName
			},
			clickBtn(){
				
				this.FormData.options = this.options
				createSubjectChoices(this.FormData).then(res=>{
					this.$modal.msgSuccess("新增成功")
					uni.navigateBack({
						delta:1
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.container{
		background-color: #fff;
		height: auto;
		padding-top: 32rpx;
		.topTitle{
			padding-left: 40rpx;
			align-items: center;
			font-weight: bold;
			color: #000000;
		}
		.textView{
			width: calc(100% - 150rpx);
			margin: 30rpx auto;
			border: #508CFF 2rpx solid;
			padding: 10rpx;
			border-radius: 10rpx;
			.textarea{
				height: 150rpx;
				width: 100%;
			}
		}
		.answerView{
			padding: 20px 30px;
			.answer{
				display: flex;
				align-items: center;
				margin-bottom: 15px;
				.add{
					width: 84%;
					height: 36px;
					font-size: 10px;
					font-family: Inter-Regular, Inter;
					color: #508CFF;
					border: #508CFF 2rpx solid;
					margin-left: 6%;
					background-color: #fff;
				}
				.option{
					width: 6%;
				}
				.text{
					width: 84%;
					height: 36px;
					border: #508CFF 2rpx solid;
					padding: 10rpx;
					border-radius: 10rpx;
				}
				.del{
					width: 10%;
					text-align: right;
				}
			}
			
			
			
		}
		.answerItem{
			display: flex;
			justify-content: space-around;
			padding: 20px;
			.item{
				display: block;
				width: 40px;
				height: 40px;
				background: rgba(80,140,255,0.49);
				border-radius: 5px 5px 5px 5px;
				color: #fff;
				text-align: center;
				line-height: 40px;
			}
			.isactive{
				background: rgba(80,140,255,1);
			}
		}
		.uni-row{
			.topTitle{
				line-height: 40px;
			}
			.text{
				height: 36px;
				border: #508CFF 2rpx solid;
				padding: 10rpx;
				border-radius: 10rpx;
				width: 80%;
				margin-left: 20%;
			}
		}
		.place{
			font-size: 12px;
		}
		.popup{
			width: 600rpx;
			height: 480rpx;
			padding: 40rpx;
			background-color: #fff;
			border-radius: 20rpx;
			.topTitle{
				padding: 0;
			}
			.text{
				height: 36px;
				border: #508CFF 2rpx solid;
				padding: 10rpx;
				border-radius: 10rpx;
				width: 90%;
				margin-left: 5%;
				margin-top: 10px;
			}
			.textarea{
				height: 72px;
			}
			.btnView{
				margin-top: 10px;
				display: flex;
				.button{
					font-size: 12px;
					width: 200rpx;
					background: rgba(80, 140, 255, 1);
					color: #fff;
					height: 36px;
					line-height: 36px;
				}
				.cancel{
					background: rgba(80, 140, 255, .5);
				}
			}

		}
		.submit-view {
			padding: 20px;
			.submit{
				color: #fff;
				width: 100px;
				height: 40px;
				font-size: 14px;
				line-height: 40px;
				background: #508CFF;
				border-radius: 5px 5px 5px 5px;
				margin: 20px auto;
			}
			
		}
		
	}
</style>